package com.alibaba.basic.util;

import com.alibaba.user.domain.LoginData;
import com.alibaba.user.domain.Logininfo;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

public class LoginContext {
    public static Logininfo getLogininfo(HttpServletRequest request){
        try {
            // 获取token
            String jwtToken = request.getHeader("token");
            // 加密
            if (jwtToken!= null){
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                //解密之后获取载荷数据
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(jwtToken, publicKey, LoginData.class);
                Logininfo logininfo = payload.getLoginData().getLogininfo();
                return logininfo;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
